package com.design.pages;
import com.design.config.GameFile;
import com.design.config.MoveBoxConfig;
import com.design.fill.FillBox;
import com.design.game.MoveBox;
import com.design.method.error.ParserError;
import com.design.util.Console;
import com.design.util.moveBox.*;
import com.design.util.moveBox.Box;
import com.design.window.Route;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.util.Date;

public class CreateMap extends Route {
    private MoveBox moveBox;
    private final CreateStats stats = new CreateStats();
    public CreateMap(String href) {
        super(href,800,800,"创建推箱子地图","创建地图");
    }
    protected void run(){}
    @Override
    protected void filling(Graphics g) {
        FillBox.fill(g,moveBox.getMethod(),800);
    }
    @Override
    protected void keyPressed(char key) {
        int[] direction = Next.switchDirection(key);
        if(direction != null){
            this.moveBox.superMove(direction);
            this.refill();
            return;
        }
        switch (key) {
                //切换方块
            case 'r' :
                this.stats.tab();
                this.refill();
            break;
                //放置方块  空格
            case 32 :
                this.moveBox.setMap(this.stats.get());
                this.refill();
            break;
                //删除方块  back
            case 8 :
                this.moveBox.setMap(Picture.getGround());
                this.refill();
            break;
                //重新创建地图 delete
            case 127 :
                File file = new File(GameFile.CREATE_MAP_FILE);
                if (!file.delete()) {
                    Console.warning(file.getAbsolutePath() + " 删除失败");
                }
                this.restart();
            break;
                //保存地图  回车
            case 10 :
                this.preserve(new File(GameFile.MAP_DIR + new Date().getTime()));
                this.setVisible(false);
                File file1 = new File(GameFile.CREATE_MAP_FILE);
                if (!file1.delete()) {
                    Console.warning(file1.getAbsolutePath() + " 删除失败");
                }
            break;
        }
    }
    private void preserve(File file){
        MapMethod method = this.moveBox.getMethod();
        method.setFile(file);
        MapParser.preserve(method);
    }
    @Override
    public boolean before() {
        File file = new File(GameFile.CREATE_MAP_FILE);
        if((file.isFile())){
            try {
                 this.moveBox = new MoveBox(MapParser.parser(file));
            } catch (ParserError parserError) {
                Console.error(parserError.getMessage());
                if(file.delete()){
                    Console.log("删除文件" + file.getAbsolutePath());
                }else{
                    Console.warning("删除文件" + file.getAbsolutePath() + " 失败");
                }
                this.restart();
            }
            return true;
        }
        int size;
        String[] str = new String[MoveBoxConfig.MAP_MAX_WIDTH - MoveBoxConfig.MAP_MIN_WIDTH + 1];
        for (int i = 0; i < str.length; i ++){
            str[i] = MoveBoxConfig.MAP_MIN_WIDTH + i + "";
        }
        String input = (String) JOptionPane.showInputDialog(this.getFrame(),"选择地图大小","选择地图大小", JOptionPane.INFORMATION_MESSAGE,null,str,str[0]);
        if(input == null){
            return false;
        }
        size = Integer.parseInt(input);
        size ++;
        Box[][] map = new Box[size + 1][size + 1];
        Picture wall = Picture.getWall();
        Picture ground = Picture.getGround();
        for(int y = 0;y <= size; y ++){
            for(int x = 0; x <= size; x ++){
                if(x == 0 || y == 0 || x == size || y == size){
                    map[y][x] = new Box(wall,x,y);
                    continue;
                }
                map[y][x] = new Box(ground,x,y);
            }
        }
        int position = size / 2;
        this.moveBox = new MoveBox(new MapMethod(map,file,position,position));
        return true;
    }
    @Override
    protected boolean close() {
        this.preserve(new File(GameFile.CREATE_MAP_FILE));
        return true;
    }
}
